package com.itheima.pinda.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.pinda.DTO.TaskPickupDispatchDTO;
import com.itheima.pinda.common.utils.PageResponse;
import com.itheima.pinda.entity.TaskPickupDispatch;
import com.itheima.pinda.enums.pickuptask.PickupDispatchTaskAssignedStatus;
import com.itheima.pinda.service.ITaskPickupDispatchService;
import com.itheima.pinda.utils.ResultUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

/**
 * <p>
 * 取件、派件任务
 * </p>
 */
@Api(tags = "取件，派件任务管理")
@Slf4j
@RestController
@RequestMapping("pickup-dispatch-task")
public class PickupDispatchTaskController {
    @Autowired
    private ITaskPickupDispatchService taskPickupDispatchService;

    /**
     * 新增取派件任务
     *
     * @param dto 取派件任务信息
     * @return 取派件任务信息
     */
    @ApiOperation(value = "新增取派件任务")
    @PostMapping("/insert")
    public TaskPickupDispatchDTO insert(@RequestBody TaskPickupDispatchDTO dto) {

        return taskPickupDispatchService.insert(dto);
    }

    /**
     * 修改取派件任务信息
     *
     * @param dto 取派件任务信息
     * @return 取派件任务信息
     */
    @PutMapping("/update")
    public TaskPickupDispatchDTO update(@RequestBody TaskPickupDispatchDTO dto) {

      return taskPickupDispatchService.update(dto);
    }

    /**
     * 获取取派件任务分页数据
     *
     * @param dto 查询条件
     * @return 取派件分页数据
     */
    @PostMapping("/page")
    public PageResponse<TaskPickupDispatchDTO> findByPage(@RequestBody TaskPickupDispatchDTO dto) {
        if (null == dto.getPage()) {
            dto.setPage(1);
        }
        if (null == dto.getPageSize()){
            dto.setPageSize(10);
        }

        IPage<TaskPickupDispatchDTO> page = taskPickupDispatchService.findByPage(dto.getPage(), dto.getPageSize(), dto);

        return ResultUtils.pageResult(dto.getPage(), dto.getPageSize(), page);
    }

    /**
     * 获取取派件任务列表
     *
     * @param dto 查询条件
     * @return 取派件任务列表
     */
    @PostMapping("/list")
    public List<TaskPickupDispatchDTO> findAll(@RequestBody TaskPickupDispatchDTO dto) {

        return taskPickupDispatchService.findAll(dto);
    }

    /**
     * 根据条件查询取派件任务详情
     *
     * @param id
     * @param taskType
     * @param orderId
     * @return
     */
    @PostMapping("/findDetail")
    public TaskPickupDispatchDTO findDetail(@RequestParam(name = "id") String id,
                                            @RequestParam(name = "taskType") Integer taskType,
                                            @RequestParam(name = "orderId") String orderId) {

        return taskPickupDispatchService.findDetail(id,taskType,orderId);
    }
}
